﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;

namespace DBA.Web.SystemManage.Permissions
{
    /// <summary>
    /// RoleTree 的摘要说明
    /// </summary>
    public class RoleTree : IHttpHandler
    {
        DataAccessLibrary dal = CommonQuery.DataAccessLibrary;

        public void ProcessRequest(HttpContext context)
        {
            string roleID = context.Request.Params["roleID"];
            List<Sys_SystemFunction> funcList = GetFunction();

            //初始化根节点

            List<TreeNode> nodeList = new List<TreeNode>();

            TreeNode nodeRoot = new TreeNode();
            nodeRoot.id = "1";
            nodeRoot.pId = "0";

            //nodeList.Add(nodeRoot);
            GetFuncTreeData(funcList, ref nodeList);
            context.Response.Write(JsonConvert.SerializeObject(nodeList));

        }

        /// <summary>
        /// 构建用户权限功能树
        /// </summary>
        /// <param name="funcList"></param>
        /// <param name="userFuncList"></param>
        /// <param name="parentFuncList"></param>
        /// <param name="strTree"></param>
        private void GetFuncTreeData(List<Sys_SystemFunction> funcList, ref List<TreeNode> nodeList)
        {

            foreach (Sys_SystemFunction func in funcList)
            {
                TreeNode node = new TreeNode();
                node.id = func.FuncID.ToString();
                node.name = func.FuncName;
                node.pId = func.ParentFuncID.ToString();
                node.title = func.FuncRemark;
                nodeList.Add(node);
            }
        }

        /// <summary>
        /// 取出当前的所有功能
        /// </summary>
        /// <returns>List<Sys_SystemFunction></returns>
        public List<Sys_SystemFunction> GetFunction()
        {
            List<Sys_SystemFunction> funcList = new List<Sys_SystemFunction>();

            string strSelect = "SELECT  FuncID,ParentFuncID,FuncName,FuncType,FuncUrl,FuncRemark";
            strSelect += " FROM Sys_SystemFunction";

            DataTable dt = dal.ExecuteFillDataTable(strSelect);
            foreach (DataRow dr in dt.Rows)
            {
                Sys_SystemFunction func = new Sys_SystemFunction();
                func.FuncID = ConvertHelper.ToInt32(dr["FuncID"]);
                func.FuncRemark = dr["FuncRemark"].ToString();
                func.FuncName = dr["FuncName"].ToString();
                if (!String.IsNullOrEmpty(dr["ParentFuncID"].ToString()))
                {
                    func.ParentFuncID = ConvertHelper.ToInt32(dr["ParentFuncID"]);
                }
                func.FuncType = ConvertHelper.ToInt16(dr["FuncType"]);
                funcList.Add(func);
            }
            return funcList;
        }

        public class TreeNode
        {
            public string id;
            public string name;
            public string pId;
            public bool nocheck;
            public bool open;
            public string unitType;
            public string title;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}